/**
 * @Author @fiber-cloud
 * @Date 2025/7/28 20:32
 * 模拟实现LinkedList
 */
public class MyLinkedList {
    //LinkedList是一个双向链表
    // prev指向的是前一个节点的地址
    // next指向的是后一个节点的位置
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;


        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;//标记双向链表的头部
    public ListNode tail;//标记双向链表的尾巴


    //从头到尾打印双向链表的每一个节点的值
    public void display1(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    //清除链表
    public void clear() {
        ListNode cur = this.head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }

    //头插法
    public void addFirst(int data) {
        ListNode d = new ListNode(data);
        //当前链表只有一个节点
        if (this.head == null){
            this.head = d;
            this.tail = d;
        }else {
            d.next = this.head;
            this.head.prev = d;
            head = d;
        }

    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
            this.tail = node;
        }else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
    }


    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws IndexWrongFulException {
        //判断index的输入是否合法
        if (index < 0 ||  index > size()){
            throw new IndexWrongFulException("index位置不合法！");
        }
        //判断特殊位置，头插、尾插
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        //在index插入值,找到index位置
        ListNode cur = findIndexListNode(index);
        ListNode node = new ListNode(data);

        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;

    }

    private ListNode findIndexListNode(int index) {
        ListNode cur = this.head;
        while (index != 0){
            cur = cur.next;
            index -- ;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        //链表为null
        if (this.head == null){
            return false;
        }else {
            while (cur != null){
                if (cur.val == key){
                    return true;
                }else {
                    cur = cur.next;
                }
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

}
